package cn.me.alphamq.client;

import cn.me.alphamq.common.entity.req.ListAllTopicInfoReq;
import cn.me.alphamq.common.entity.resp.ListAllTopicInfoResp;
import cn.me.alphamq.common.enumeration.BrokerRoleEnum;
import cn.me.alphamq.common.enumeration.EventCodeEnum;
import cn.me.alphamq.common.enumeration.ServiceInstanceTypeEnum;
import cn.me.alphamq.common.msg.TcpMsg;
import cn.me.alphamq.common.remote.DefaultClient;
import cn.me.alphamq.config.NameServerProperties;
import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Component;

/**
 * 控制台的默认客户端
 *
 * @author f
 */
@Component
public class DefaultConsole extends DefaultClient {

    /**
     * 发送列举所有 topic 信息的消息
     *
     * @return 所有的 topic 信息
     */
    public ListAllTopicInfoResp sendListAllTopicInfoMsg() {
        TcpMsg resp = nameServerClient.sendMsgSync(EventCodeEnum.LIST_ALL_TOPIC_INFO_REQ, new ListAllTopicInfoReq());
        return JSON.parseObject(resp.getBody(), ListAllTopicInfoResp.class);
    }

    public DefaultConsole(NameServerProperties nameServerProperties) {
        super(BrokerRoleEnum.MASTER.getCode(), "test_group",
                nameServerProperties.getIp(), nameServerProperties.getPort(),
                "default-console-nameserver-client");

        super.start(nameServerProperties.getUsername(), nameServerProperties.getPassword(),
                ServiceInstanceTypeEnum.CONSUMER);
    }

    @Override
    protected String genBrokerClientName(String brokerAddress) {
        return "default-console-nameserver-client-on[" + brokerAddress + "]";
    }
}
